//
//  ViewController.m
//  Shadow
//
//  Created by alpha Feng on 14-8-14.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    UIButton *btn = [[UIButton alloc] initWithFrame:self.showImageView.bounds];
    [btn setFrame:CGRectMake(20, 20, 40, 40)];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn setTitle:@"start" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
//    [self performSelector:@selector(showAnimation) withObject:nil afterDelay:1.0];
    
    [self.bgImageView setBackgroundColor:[UIColor clearColor]];
    
    [self.showImageView addSubview:self.bgImageView];
    
    [self.showImageView.layer setMasksToBounds:YES];
    [self.showImageView.layer setBorderColor:[UIColor grayColor].CGColor];
    [self.showImageView.layer setBorderWidth:1.0];
    [self.showImageView.layer setCornerRadius:75.0];
    
    [self.bgImageView1 setBackgroundColor:[UIColor clearColor]];
    
    [self.showImageView1 addSubview:self.bgImageView1];
    
    [self.showImageView1.layer setMasksToBounds:YES];
    [self.showImageView1.layer setBorderColor:[UIColor grayColor].CGColor];
    [self.showImageView1.layer setBorderWidth:1.0];
    [self.showImageView1.layer setCornerRadius:75.0];
    
}

/*!
 *  聚焦投射动画
 *
 *  @param bgImageView          需要做聚焦投射的大图
 *  @param showImageView        聚焦投射的范围
 *  @param points               移动的位置(关键帧)数组
 *  @param sumTime              总动画时间
 *  @param times                时间数组（关键帧之间的动画时间）count（times）= count（points）-1
 *
 *  @return void
 */
- (void)focusAnimationWithBgImageView:(UIImageView *) bgImageView
                        showImageview:(UIImageView *) showImageView
                               points:(NSArray *) points
                        intervalTimes:(NSArray *) intervalTimes
{
    float sumTime = 0;
    for (NSNumber *intervalTime in intervalTimes) {
        sumTime = sumTime + [intervalTime floatValue];
    }
    if (sumTime == 0) {
        // 动画时间按为0，不做动画
        return;
    }else{
        NSMutableArray *times = [[NSMutableArray alloc] init];
        [times addObject:[NSNumber numberWithFloat:0.0]];
        float tempSumTime = 0.0;
        for (NSNumber *intervalTime in intervalTimes) {
            // 计算当前间隔时间在整个动画时间中的比例
            float tempIntervalTime = [intervalTime floatValue]/sumTime;
            [times addObject:[NSNumber numberWithFloat:(tempSumTime + tempIntervalTime)]];
            tempSumTime = tempSumTime + tempIntervalTime;
        }
        [self focusAnimationWithBgImageView:bgImageView showImageview:showImageView points:points sumTime:sumTime times:times];
    }
}


/*!
 *  聚焦投射动画
 *
 *  @param bgImageView          需要做聚焦投射的大图
 *  @param showImageView        聚焦投射的范围
 *  @param points               移动的位置(关键帧)数组
 *  @param sumTime              总动画时间
 *  @param times                时间数组（位置数组跟时间数组是对应的，一个时间出现一个位置，中间动画用直线动画补全，时间从0开始,1.0结束）
 *                              如果时间为nil，每个关键帧动画平分动画时间
 *
 *  @return void
 */

- (void)focusAnimationWithBgImageView:(UIImageView *) bgImageView
                        showImageview:(UIImageView *) showImageView
                               points:(NSArray *) points
                              sumTime:(float) sumTime
                                times:(NSArray *) times
{
    
    // 计算bg动画的points
    NSMutableArray *bgPoints = [[NSMutableArray alloc] init];
    NSMutableArray *showPoints = [[NSMutableArray alloc] init];
    for (NSValue *value in points) {
        CGPoint tempPoint = [value CGPointValue];
        CGPoint point = CGPointMake(bgImageView.center.x-tempPoint.x, bgImageView.center.y-tempPoint.y);
        [bgPoints addObject:[NSValue valueWithCGPoint:point]];
        
        point = CGPointMake(showImageView.center.x+tempPoint.x, showImageView.center.y+tempPoint.y);
        [showPoints addObject:[NSValue valueWithCGPoint:point]];
    }
    
    
    CAKeyframeAnimation  *bgAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [bgAnimation setValues:bgPoints];
    [bgAnimation setDuration:sumTime];
    [bgAnimation setRemovedOnCompletion:NO];
    [bgAnimation setKeyTimes:times];
    [bgAnimation setFillMode:kCAFillModeForwards];
    
    [bgImageView.layer addAnimation:bgAnimation forKey:NULL];
    
    CAKeyframeAnimation *showAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [showAnimation setValues:showPoints];
    [showAnimation setDuration:sumTime];
    [showAnimation setRemovedOnCompletion:NO];
    [showAnimation setKeyTimes:times];
    [showAnimation setFillMode:kCAFillModeForwards];
    
    [showImageView.layer addAnimation:showAnimation forKey:NULL];

}

- (void)click
{
    NSLog(@"click");
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(50, 0)]];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(50, 100)]];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(0, 100)]];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(0, 75)]];
    [arr addObject:[NSValue valueWithCGPoint:CGPointMake(25, 75)]];
    
//    NSMutableArray *timesArr = [[NSMutableArray alloc] init];
//    [timesArr addObject:[NSNumber numberWithFloat:0.0]];
//    [timesArr addObject:[NSNumber numberWithFloat:0.1]];
//    [timesArr addObject:[NSNumber numberWithFloat:0.3]];
//    [timesArr addObject:[NSNumber numberWithFloat:0.6]];
//    [timesArr addObject:[NSNumber numberWithFloat:0.7]];
//    [timesArr addObject:[NSNumber numberWithFloat:1.0]];
//    
//    [self focusAnimationWithBgImageView:self.bgImageView showImageview:self.showImageView points:arr sumTime:3.0 times:timesArr];
    
    NSMutableArray *timesArr = [[NSMutableArray alloc] init];
    [timesArr addObject:[NSNumber numberWithFloat:0.3]];
    [timesArr addObject:[NSNumber numberWithFloat:0.6]];
    [timesArr addObject:[NSNumber numberWithFloat:0.9]];
    [timesArr addObject:[NSNumber numberWithFloat:0.3]];
    [timesArr addObject:[NSNumber numberWithFloat:0.9]];
    
    [self focusAnimationWithBgImageView:self.bgImageView showImageview:self.showImageView points:arr intervalTimes:timesArr];
    
    NSMutableArray *arr1 = [[NSMutableArray alloc] init];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(-50, 0)]];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(-50, 100)]];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(0, 100)]];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(0, 75)]];
    [arr1 addObject:[NSValue valueWithCGPoint:CGPointMake(-25, 75)]];
    
    [self focusAnimationWithBgImageView:self.bgImageView1 showImageview:self.showImageView1 points:arr1 intervalTimes:timesArr];
    
    
}

- (void)showAnimation
{
//    [UIView animateWithDuration:2.0 animations:^{
//        CGRect frame = self.showImageView.frame;
//        frame.origin.x = frame.origin.x +100;
//        [self.showImageView setFrame:frame];
//    }];
    
    CGPoint pt0 = CGPointMake(75.0, 75.0);
    CGPoint pt1 = CGPointMake(75.0, 100.0);
    CGPoint pt2 = CGPointMake(150.0, 100.0);
    CGPoint pt3 = CGPointMake(150.0, 75.0);

    NSArray *vv = [[NSArray alloc] initWithObjects:[NSValue valueWithCGPoint:pt0],
                       [NSValue valueWithCGPoint:pt1],
                       [NSValue valueWithCGPoint:pt2],
                       [NSValue valueWithCGPoint:pt3],
//                       [NSValue valueWithCGPoint:pt4],
//                       [NSValue valueWithCGPoint:pt5],
//                       [NSValue valueWithCGPoint:pt6],
//                       [NSValue valueWithCGPoint:pt7],
//                       [NSValue valueWithCGPoint:pt8],
//                       [NSValue valueWithCGPoint:pt9],
//                       [NSValue valueWithCGPoint:pt10],
//                       [NSValue valueWithCGPoint:pt11],
//                       [NSValue valueWithCGPoint:pt12],
                       nil];
    
    NSArray *times = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
                                                [NSNumber numberWithFloat:0.4],
                                                [NSNumber numberWithFloat:0.6],
                                                [NSNumber numberWithFloat:1.0],
                                                nil];
    
    CAKeyframeAnimation  *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [animation setValues:vv];
    [animation setDuration:3.0];  
//    [animation setAutoreverses:YES];
    [animation setKeyTimes:times];
    // 保留最后动画状态的方法2，设置动画属性
//    [animation setRemovedOnCompletion:NO];
//    [animation setFillMode:kCAFillModeForwards];
    
    [self.bgImageView.layer addAnimation:animation forKey:NULL];
    // 保留最后动画状态的方法2，直接移动
//    self.showImageView.center = pt3;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
